package com.example.teaching_server.controller;

import com.example.teaching_server.common.ApiResponse;
import com.example.teaching_server.dto.LoginDTO;
import com.example.teaching_server.dto.LoginResultDTO;
import com.example.teaching_server.service.AuthService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Tag(name = "认证管理", description = "用户认证相关接口")
public class AuthController {

    private final AuthService authService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    @Operation(summary = "用户登录", description = "用户登录认证接口")
    public ApiResponse<LoginResultDTO> login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录请求: username={}", loginDTO.getUsername());

        try {
            LoginResultDTO result = authService.login(loginDTO);
            log.info("用户登录成功: username={}", loginDTO.getUsername());
            return ApiResponse.success("登录成功", result);
        } catch (Exception e) {
            log.error("用户登录失败", e);
            return ApiResponse.fail("登录失败: " + e.getMessage());
        }
    }
}
